package com.exam.controller;

import com.exam.dto.ActionResult;
import com.exam.po.ExamManage;
import com.exam.po.Student;
import com.exam.service.StudentMannagerService;
import com.exam.utils.QiniuUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhanghui
 * @ClassName StuentMannagerController
 * @Description TODO
 * @Date 2021/1/12 11:33
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api")
public class StuentMannagerController {
    @Autowired
    private StudentMannagerService studentMannagerService;
    /**
     * @Author zhanghui
     * @Description 图片上传到七牛云
     * @Date 2021/1/16 16:26
     * @Param [file]
     * @return com.exam.dto.ActionResult
     **/
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public ActionResult uploadFile(@RequestParam("file") MultipartFile file){
        ActionResult result = new ActionResult();
        result.setCode(200);
//        上传文件
        String url = QiniuUpload.uploadFile(file);
        result.setData(url);
        return new ActionResult(200,"上传成功",url);
    }

    /**
     * @Author zhanghui
     * @Description 添加学生
     * @Date 2021/1/12 11:46
     * @Param [student]
     * @return com.exam.dto.ActionResult
     **/
    @PostMapping("/admin/addStudents")
    public ActionResult addStudent(@RequestBody Student student){
        if(student==null) {
            return new ActionResult(101, "添加失败", null);
        }else if(student.getStudentname()==null||student.getPwd()==null||student.getStudentname()==" "||student.getPwd()==" ") {
            return new ActionResult(102, "用户名或密码不能为空", null);
        } else{
            studentMannagerService.addStudent(student);
            return new ActionResult(200,"添加成功",null);
        }
    }
    /**
     * @Author zhanghui
     * @Description 删除学生
     * @Date 2021/1/12 13:46
     * @Param [id]
     * @return com.exam.dto.ActionResult
     **/
    @DeleteMapping("/admin/deleteStudent/{studentid}")
    public ActionResult deleteStudent(@PathVariable Integer studentid){


        if(studentMannagerService.deleteStudent(studentid)>0){
            return new ActionResult(200,"删除成功",null);
        }else{
            return new ActionResult(400,"删除失败",null);
        }
    }
    /**
     * @Author zhanghui
     * @Description 修改用户
     * @Date 2021/1/12 13:49
     * @Param [student]
     * @return com.exam.dto.ActionResult
     **/
    @PutMapping("/admin/updateStudent")
    public ActionResult updateStudent(@RequestBody Student student){
        if(studentMannagerService.updateStudent(student)>0&&student!=null){
            return new ActionResult(200,"修改成功",null);
        }
        return new ActionResult(400,"修改失败",null);
    }
    /**
     * @Author zhanghui
     * @Description 学生分页查询
     * @Date 2021/1/12 14:07
     * @Param [page, size]
     * @return com.exam.dto.ActionResult
     **/
    @GetMapping("/admin/checkStudentByPage/{page}/{size}")
    public ActionResult checkStudentByPage(@PathVariable Integer page,@PathVariable Integer size){
        Map<String,Object> map = new HashMap<>();
        Integer total = studentMannagerService.findStudentCounts();
        List<Student> res = studentMannagerService.findStudentByPage(page,size);
        map.put("total",total);
        map.put("data",res);
        if(map!=null){
            return new ActionResult(200,"分页成功",map);
        }
        return new ActionResult(400,"分页失败",null);
    }
    /**
     * @Author zhanghui
     * @Description 查询试卷考试
     * @Date 2021/1/13 17:52
     * @Param [teacherId, page, size]
     * @return com.exam.dto.ActionResult
     **/
    @GetMapping("/teacher/checkStudentExam/{teacherId}/{page}/{size}")
    public ActionResult checkStudentExam(@PathVariable Integer teacherId,@PathVariable Integer page,@PathVariable Integer size){
        Map<String,Object> map = new HashMap<>();
        Integer total = studentMannagerService.findStudentExamCount(teacherId);
        List<ExamManage> res = studentMannagerService.findStudentExam(teacherId,page,size);
        map.put("total",total);
        map.put("data",res);
        if(map != null){
            return new ActionResult(200,"查询授课老师对应的试卷成功",map);
        }
        return new ActionResult(400,"查询失败",null);
    }

}
